package com.example.aigc_education.integration.response.workflows.v1;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

@Data
public class CozeStudyPlanResponse implements Serializable {

    @JsonProperty("content_type")
    private Integer content_type;

    @JsonDeserialize(using = StudyPlanDataDeserializer.class)
    private StudyPlanData data;

    @JsonProperty("original_result")
    private Object original_result;

    @JsonProperty("type_for_model")
    private Integer type_for_model;

    @Data
    public static class StudyPlanData implements Serializable{
        private String name;

        private List<Plan> plan;
    }

    @Data
    public static class Plan implements Serializable{
        private String topic;

        private List<String> subtopics;
    }

    public static class StudyPlanDataDeserializer extends JsonDeserializer<StudyPlanData> {
        @Override
        public CozeStudyPlanResponse.StudyPlanData deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            ObjectMapper mapper = (ObjectMapper) p.getCodec();
            JsonNode node = p.readValueAsTree();
            CozeStudyPlanResponse.StudyPlanData studyPlanData = new CozeStudyPlanResponse.StudyPlanData();

            if (node.isTextual()) {
                String jsonString = node.asText();
                studyPlanData = mapper.readValue(jsonString, mapper.getTypeFactory().constructType(CozeStudyPlanResponse.StudyPlanData.class));
            }

            return studyPlanData;
        }
    }
}
